<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

define("DISASTER_EVENTS_TABLE", "disaster_events");
define("SUPPLIES_TABLE", "supplies");
define("SHIPMENTS_TABLE", "shipments");
define("USER_CONTRIBUTIONS_TABLE", "user_contributions");

function db_event_add($array)
{

/*select wordpress database defined in the config file and select the table, 
then import the provided variables into the table*/

    global $wpdb;
					
    $location_name = mysql_real_escape_string($array["location"]);
    $geocode = mysql_real_escape_string($array["geocode"]);
    $name_of_event = mysql_real_escape_string($array["name"]);
    $s_date = mysql_real_escape_string($array["start_date"]);
    $desc = mysql_real_escape_string($array["description"]);
    $shp_address = mysql_real_escape_string($array["shipping_address"]);

   $table_name = $wpdb->prefix . DISASTER_EVENTS_TABLE;

   $rows_affected = $wpdb->insert( $table_name, array( 'location' => $location_name,
				    'geocode' => $geocode, 'name' => $name_of_event,
				    'start_date' => $s_date, 'description' => $desc,
				    'shipping_address' => $shp_address) );

}


function db_event_get($id)
{
/* As of 3/3/11 update event_view working correctly - N/A*/

    global $wpdb;

    $table_name = $wpdb->prefix . DISASTER_EVENTS_TABLE;

    $sql = $wpdb->get_results("SELECT * FROM " . $table_name ." WHERE event_id = " . $id, ARRAY_A);
    return $sql;

}

function db_event_list($flag)
{
/* As of 3/3/11 event_list working correctly - N/A*/

    global $wpdb;

    $table_name = $wpdb->prefix . DISASTER_EVENTS_TABLE;
    $flag = $flag ? '1' : '0'; // TRUE and FALSE does not seem to work in the query

    $sql = $wpdb->get_results("SELECT * FROM " . $table_name ." WHERE remove_flag = " . $flag, ARRAY_A);
    return $sql;


}


function db_event_locations($active_flag)
{

global $wpdb;
$false = 0;

$table_name = $wpdb->prefix . DISASTER_EVENTS_TABLE;

$sql = $wpdb->get_results("SELECT name, geocode, event_id FROM " . $table_name . " WHERE active_flag = 1 AND remove_flag = 0", ARRAY_A);
return $sql;

}



function db_event_update($array)
{
/* As of 3/3/11 update function working correctly - N/A*/

global $wpdb;

$table_name = $wpdb->prefix . DISASTER_EVENTS_TABLE;


$event_id = $array["event_id"];
$loc = $array["location"];
$geo = $array["geocode"];
$name = $array["name"];
$s_date = $array["start_date"];
$desc = $array["description"];
$shp_add = $array["shipping_address"];
$e_date = $array["end_date"];



    $wpdb->update($table_name, array( 'location' => $loc, 'geocode' => $geo, 'name' => $name, 'start_date' => $s_date, 'description' => $desc, 'shipping_address' => $shp_add, 'end_date' => $e_date  ), array('event_id' => $event_id));

	}



function db_event_delete($record_id)
{
/*send in the record id of a record that you want to move to the trash
* the BOOL remove_flag will be set and it will be flagged to be in the 
* trash, while not completely being removed from the database.
*/
global $wpdb;
    $flag = 1; // TRUE and FALSE does not seem to work in the query
$table_name = $wpdb->prefix . DISASTER_EVENTS_TABLE;
$wpdb->update( $table_name, array( 'remove_flag' => $flag), array('event_id' => $record_id));
}

function db_event_undelete_record($record_id)
{
/* Brings record back from the "trash" by reverting the flag set by remove_record
 * and allowing that record to be shown. 
 */
global $wpdb;
    $flag = 0;  // TRUE and FALSE does not seem to work in the query
$table_name = $wpdb->prefix . DISASTER_EVENTS_TABLE;
$wpdb->update( $table_name, array( 'remove_flag' => $flag), array('event_id' => $record_id));
}

function db_shipment_get($shipment_id)
{
    global $wpdb;

    $table_name = $wpdb->prefix .SHIPMENTS_TABLE;

    $sql = $wpdb->get_results("SELECT * FROM " . $table_name ." WHERE shipmet_id = " . $shipment_id, ARRAY_A);
    return $sql;
}


function db_shipments_by_event($event_id)
{

    global $wpdb;

    $table_name = $wpdb->prefix .SHIPMENTS_TABLE;

    $sql = $wpdb->get_results("SELECT * FROM " . $table_name ." WHERE event_id = " . $event_id, ARRAY_A);
    return $sql;

}

function db_shipment_add($array)
{
    global $wpdb;

    $date_sent = mysql_real_escape_string($array["date_sent"]);
    $date_received = mysql_real_escape_string($array["date_received"]);
    $event_id = mysql_real_escape_string($array["event_id"]);


   $table_name = $wpdb->prefix . SHIPMENTS_TABLE_TABLE;

   $rows_affected = $wpdb->insert( $table_name, array( 'event_id' => $event_id,
				    'date_sent' => $date_sent, 'date_received' => $date_received) );

}

function db_shipment_update($array)
{
	global $wpdb;

$table_name = $wpdb->prefix . SHIPMENTS_EVENTS_TABLE;


$event_id = $array["event_id"];
$shipment_id = $array["shipment_id"];
$date_sent = $array["date_sent"];
$date_recieved = $array["date_recieved"];



    $wpdb->update($table_name, array( 'event_id' => $event_id, 'shipment_id' => $shipment_id, 'date_sent' => $date_sent, 'date_recieved' => $date_recieved), array('event_id' => $event_id));




}



function db_supplies_by_shipment($shipment_id)
{
	    global $wpdb;

    $table_name = $wpdb->prefix .SUPPLIES_TABLE;

    $sql = $wpdb->get_results("SELECT * FROM " . $table_name ." WHERE shipment_id = " . $shipment_id, ARRAY_A);
    return $sql;


}

function db_supplies_total($type, $event_id)
{
	    global $wpdb;

$sql = $wpdb->get_results("SELECT wp_supplies.type, wp_supplies.amount_needed, SUM(wp_user_contributions.amount) AS SUM FROM wp_supplies, wp_disaster_events, wp_user_contributions, wp_shipments WHERE wp_supplies.event_id = wp_disaster_events.event_id AND wp_shipments.shipment_id = wp_supplies.shipment_id AND wp_supplies.shipment_id = wp_user_contributions.shipment_id AND wp_user_contributions.type = wp_supplies.type AND wp_disaster_events.event_id = '". $event_id . "' AND wp_supplies.type = '" . $type . "'", ARRAY_A);

    return $sql;


}



?>
